home *** CD-ROM | disk | FTP | other *** search
- Path: rain.fr!world-net!usenet
- From: Frederic LACHASSE <lachass@worldnet.fr>
- Newsgroups: comp.lang.c++
- Subject: Re: I need help please
- Date: Fri, 29 Mar 1996 20:37:51 +0000
- Organization: World-Net information exchange, Internet provider.
- Message-ID: <VA.00000074.00030e6a@fred>
- References: <4ja6n4$fh2@dfw-ixnews4.ix.netcom.com>
- Reply-To: lachass@worldnet.fr
- NNTP-Posting-Host: client144.sct.fr
- X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
-
- In article <4ja6n4$fh2@dfw-ixnews4.ix.netcom.com>, lewkbj@ix.netcom.com
- (leonel wizel ) wrote:
- >
- > I need some help please,
- >
- > I have to write a writing check program, that takes the numbers for
- > example (112.43) and write as a: one hundred and 43/100 output
- >
- > I had the idea, and I thought had the algorithms, but the program that
- > I wrote does not work properly.
- >
- > I need some help or suggestions of how to make this program work.
- >
- >
-
- Most of your problems come from using double variables where you
- actually need integers.
-
- Short explanation: 1/3 cannot be represented by an exact decimal number
- (0.3333333333 is just an approximation to the 10th decimal), so
- computing 3 * (1/3) will probably give 0.9999999999 with a computer
- using decimal floating point arithmetic with a precision of 10 digits.
-
- Most micro-computers use *binary* floating point numbers and values like
- 0.1 (1/10) cannot be represented exactly with a binary floating point
- number. So expression like (x / 1000.0 * 1000.0 == x) are *not*
- guaranteed to be true.
-
- So the best course is to convert your number to integers as soon as
- possible and do all your arithmetics with integers afterward.
-
- So:
-
- double checkvalue;
- cin >> checkvalue;
- int amount = int(checkvalue + 0.001); // just help the thing to
- // round properly
- int change = int((checkvalue - amount) * 100.0 + 0.1);
-
- // Now use % and / on integer to do the job.
-
- After that, you'll never have strange behavior from arithmetics. The
- remaining bugs^H^H^H^Hproblems will be your complete responsability.
-
- I hope this'll help.
-
- Frederic LACHASSE (ECP 86)
- CompuServe: 100530,2005
- Internet: lachass@worldnet.fr
-
-